<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<html:html>
<head>	
	<link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/css/sampleForm.css" />
	<link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/css/me.css" />
	<link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/common/css/help/help.css" />	
	<title>บันทึกใบซื้ออุปกรณ์, วัตถุดิบ (BCAPInvoice, BCAPInvoiceSub)</title>
	<script type='text/javascript' src='/ittl/dwr/interface/BCProjectService.js'></script>
  	<script type='text/javascript' src='/ittl/dwr/interface/ItemLocationService.js'></script>
  	<script type='text/javascript' src='/ittl/dwr/interface/BCAPInvoiceService2.js'></script>
  	<script type='text/javascript' src='/ittl/dwr/interface/po.js'></script>
  	
  	<script type='text/javascript' src='/ittl/dwr/engine.js'></script>
  	<script type='text/javascript' src='/ittl/dwr/util.js'></script>

	<script type='text/javascript' src='<%= request.getContextPath() %>/css/autocomplete/lib/jquery.js' ></script>
	<script type='text/javascript' src='<%= request.getContextPath() %>/css/autocomplete/lib/jquery.bgiframe.min.js1' ></script>
	<script type='text/javascript' src='<%= request.getContextPath() %>/css/autocomplete/lib/jquery.ajaxQueue.js1' ></script>
	<script type='text/javascript' src='<%= request.getContextPath() %>/css/autocomplete/lib/thickbox-compressed.js1' ></script>
	<script type='text/javascript' src='<%= request.getContextPath() %>/css/autocomplete/lib/jquery.autocomplete.js' ></script>
		
	
	<link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/css/autocomplete/css/mainOLD.css" />
	<link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/css/autocomplete/css/jquery.autocomplete.css" />
	<link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/css/autocomplete/css/thickbox.css" />
 
 	<script type="text/javascript" src="<%= request.getContextPath() %>/stockRequest/jquery.validate.js" ></script>
 	<script type="text/javascript" src="<%= request.getContextPath() %>/stockRequest/jquery-1.4.2.min1.js" ></script>

<!-- start 
 
	<link type="text/css" href="<%= request.getContextPath() %>/jquery-ui-1.8/development-bundle/themes/base/jquery.ui.all.css" rel="stylesheet" />
	<link type="text/css" href="<%= request.getContextPath() %>/jquery-ui-1.8/development-bundle/demos/demos.css" rel="stylesheet" />
	  	
	<script type="text/javascript" src="<%= request.getContextPath() %>/jquery-ui-1.8/development-bundle/jquery-1.4.2.js"></script>
	<script type="text/javascript" src="<%= request.getContextPath() %>/jquery-ui-1.8/development-bundle/external/jquery.bgiframe-2.1.1.js"></script>			
	<script type="text/javascript" src="<%= request.getContextPath() %>/jquery-ui-1.8/development-bundle/ui/jquery.ui.core.js"></script>
	<script type="text/javascript" src="<%= request.getContextPath() %>/jquery-ui-1.8/development-bundle/ui/jquery.ui.widget.js"></script>
	<script type="text/javascript" src="<%= request.getContextPath() %>/jquery-ui-1.8/development-bundle/ui/jquery.ui.mouse.js"></script>
	<script type="text/javascript" src="<%= request.getContextPath() %>/jquery-ui-1.8/development-bundle/ui/jquery.ui.button.js"></script>
	<script type="text/javascript" src="<%= request.getContextPath() %>/jquery-ui-1.8/development-bundle/ui/jquery.ui.draggable.js"></script>
	<script type="text/javascript" src="<%= request.getContextPath() %>/jquery-ui-1.8/development-bundle/ui/jquery.ui.position.js"></script>
	<script type="text/javascript" src="<%= request.getContextPath() %>/jquery-ui-1.8/development-bundle/ui/jquery.ui.resizable.js"></script>
	<script type="text/javascript" src="<%= request.getContextPath() %>/jquery-ui-1.8/development-bundle/ui/jquery.ui.dialog.js"></script>
	<script type="text/javascript" src="<%= request.getContextPath() %>/jquery-ui-1.8/development-bundle/ui/jquery.effects.core.js"></script>
	<script type="text/javascript" src="<%= request.getContextPath() %>/jquery-ui-1.8/development-bundle/ui/jquery.ui.datepicker.js"></script>
	<script type="text/javascript" src="<%= request.getContextPath() %>/jquery-ui-1.8/development-bundle/ui/jquery.ui.autocomplete.js"></script>
			
-->
<!-- end -->

<!-- start add/remove  
	<script type="text/javascript" src='<%= request.getContextPath() %>/common/js/addremove/jquery-latest.pack.js'></script>	
-->	
	<script type="text/javascript" src='<%= request.getContextPath() %>/common/js/addremove/jquery.highlightFade.js'></script>
	<script type="text/javascript" >
	function modalWinItem(aa) {
		if (window.showModalDialog) {		
			window.showModalDialog(aa,"name","dialogTop:100px;dialogWidth:650px;dialogHeight:350px;center:yes");
		} else {		
			window.open(aa,'name','height=350,width=600,toolbar=no,directories=no,status=no,continued from previous linemenubar=no,scrollbars=no,resizable=no ,modal=yes');
		}
	}
	
	</script>	
	<script type="text/javascript">
		
	var id = 1;	

	var idArray = new Array();
	idArray[0] = 1;
	for(var i=1; i<=100; i++) {
		idArray[i] = 0;	
	}
	
	var max = false;
	
	function addFormField() {
		max = true;
		for (i=0; i<idArray.length; i++) {
			if(idArray[i]==0) {
				max = false;
				idArray[i] = 1;
				id = i+1; 
				i = idArray.length;

				//alert(id);
				
				var newInput = "<tr id='r"+id+"'>"+
								"<td><a href='#' onClick='removeFormField(\"#r" + id + "\","+id+"); return false;'>"+
									"<img src=\"<%=request.getContextPath() %>/common/images/remove.png\" width=\"24\" height=\"24\" alt=\"Remove\" /></a></td>"+													
								  	"<td>"+	id+			  						  						  		
							  			"<input type='text' name='docNoArray' size='15' id='docNo"+id+"' onblur='getBCAPInvoice("+id+");' onchange='getBCAPInvoice("+id+");' >"+				  		
							  		"</td>"+
							  		"<td align='center'>"+
						  				"<input type='text' name='typeArray' id='type"+id+"' maxlength='3' size='5' onfocus='addFormField(); return false;' >"+
						  			"</td>"+							  		
								  	"<td align='right' valign='middle'>"+
							  			"<div align='right' id='items"+id+"'></div>"+ 
							  		"</td>"+
							  		"<td align='right' valign='middle'>"+
							  			"<div align='right' id='cash"+id+"'></div>"+ 
							  		"</td>"+							  		
							  		"<td align='right' valign='middle'>"+
							  			"<div align='right' id='sumOfItemAmount"+id+"'></div>"+ 
							  		"</td>"+
							  		"<td align='right' valign='middle'>"+
							  			"<div align='right' id='taxAmount"+id+"'></div>"+ 
							  		"</td>"+
							  		"<td align='right' valign='middle'>"+
							  			"<div align='right' id='totalAmount"+id+"'></div>"+ 
							  		"</td>"+				  					  	

							  		
								"</tr>";
				//alert(id);
				//alert(newInput);
				$("#inputs").append(newInput); 
				$('#r' + id).highlightFade({
					speed:2000
				});
				//document.getElementById("docNo"+id).focus();														
			}
		}
						
		if(max==true) {											 	
			alert("คุณสามารถเพิ่มข้อมูลได้สูงสุด 100 รายการ");
		}				
	}
	
	function removeFormField(rid, id) {
		if(confirm('ต้องการลบแถว หรือไม่ ?')) {
			var count = 0;
			for (i=0; i<idArray.length; i++) {
				if(idArray[i]>0) {
					count++;
				}
			}		
			if(count>1) {
				$(rid).remove();
				idArray[id-1] = 0;
			}else {
				alert("กรุณาบันทึกข้อมูลอย่างน้อย 1 รายการ");
			}
		}
	}
		
	</script>
<!-- end add/remove  -->

<style type="text/css">
#form1 label.error {
	font-size: 1.0em;
	color: red;
}
</style>
	<script type="text/javascript">
		$(document).ready(function(){
				//alert('b');
    			$("#reportDate").datepicker({ altField: 'input#reportDate', altFormat: 'yy-mm-dd' });
  		});
	</script> 	

<!--  ส่วนที่ 2 -->
<script language="JavaScript">
var inputNo;
var itemName = "";
var code;
var itemName;
var whCode;
var shelfCode;
var shelfName;

function getData(var1,x) {	
	inputNo = x;
	ItemLocationService.getData( var1, loadinfo );	  
}

//<!--  ส่วนที่ 3 -->
function loadinfo(data)
{
	code 	  = data.code;
	itemName  = data.name1;
	whCode 	  = data.whCode;
	shelfCode = data.shelfCode;
	shelfName = data.shelfName;
	unitCode = data.unitCode;
	unitName = data.unitName;

	ittl_row = data.ittl_row;
	ittl_shelf = data.ittl_shelf;
	
	switch(inputNo) {
	case 1:	dwr.util.setValue("docNo1", code);
			dwr.util.setValue("itemName1", itemName);
			dwr.util.setValue("whCode1", whCode);
			dwr.util.setValue("whCode11", whCode);
			dwr.util.setValue("unitCode1", unitCode);
			dwr.util.setValue("unitName1", unitName);
			//redirectN(0, document.getElementById('shCode1'));
			
			dwr.util.setValue("ittl_row1", ittl_row);
			dwr.util.setValue("ittl_shelf1", ittl_shelf);
								
			break;
	case 2:	dwr.util.setValue("docNo2", code);
			dwr.util.setValue("itemName2", itemName);
			dwr.util.setValue("whCode2", whCode);			
			dwr.util.setValue("unitCode2", unitCode);
			dwr.util.setValue("unitName2", unitName);
			//redirectN(0, document.getElementById('shCode2'));
			
			dwr.util.setValue("ittl_row2", ittl_row);
			dwr.util.setValue("ittl_shelf2", ittl_shelf);
									
			break;
	case 3:	dwr.util.setValue("docNo3", code);
			dwr.util.setValue("itemName3", itemName);
			dwr.util.setValue("whCode3", whCode);			
			dwr.util.setValue("unitCode3", unitCode);
			dwr.util.setValue("unitName3", unitName);
			//redirectN(0, document.getElementById('shCode3'));
			
			dwr.util.setValue("ittl_row3", ittl_row);
			dwr.util.setValue("ittl_shelf3", ittl_shelf);
									
			break;
	case 4:	dwr.util.setValue("docNo4", code);
			dwr.util.setValue("itemName4", itemName);
			dwr.util.setValue("whCode4", whCode);			
			dwr.util.setValue("unitCode4", unitCode);
			dwr.util.setValue("unitName4", unitName);
			//redirectN(0, document.getElementById('shCode4'));
			
			dwr.util.setValue("ittl_row4", ittl_row);
			dwr.util.setValue("ittl_shelf4", ittl_shelf);						
			break;
	case 5:	dwr.util.setValue("docNo5", code);
			dwr.util.setValue("itemName5", itemName);
			dwr.util.setValue("whCode5", whCode);			
			dwr.util.setValue("unitCode5", unitCode);
			dwr.util.setValue("unitName5", unitName);
			//redirectN(0, document.getElementById('shCode5'));//ใช้ document.form1.shelfCode5 จะทำงานได้บน IE แต่ไม่ไได้บน FF

			dwr.util.setValue("ittl_row5", ittl_row);
			dwr.util.setValue("ittl_shelf5", ittl_shelf);			
			break;
	}		
}

function redirectN(x, tempN){
	
	var groupsN=5;//document.form1.shelfCode1.options.length
	var groupN=new Array(groupsN);
	for (i=0; i<groupsN; i++)
		groupN[i]=new Array();

	//var tempN=document.form1.shelfCode1;	
	//alert("tempN.options.length-1 = "+(tempN.options.length-1));
	for (m=tempN.options.length-1;m>=0;m--) {
		//alert(tempN.options[m]);
		tempN.options[m]=null;
	}
	
	if(shelfName != null) {
		for (i=0; i<shelfName.length; i++) {
			groupN[0][i]=new Option(shelfName[i], shelfCode[i]);
		}
					
		for (i=0;i<groupN[x].length;i++){
			text = groupN[x][i].value+":"+groupN[x][i].text;
			tempN.options[i]=new Option(text,groupN[x][i].value);
		}
		tempN.options[0].selected=true;
	}
}
</script>
//<!--  ส่วนที่ 2 -->
<script language="JavaScript">

function getProject() {
	//alert("bcproject"+form1.projectCode.value); ใช้ใน ff ไม่ผ่าน			
	BCProjectService.getProject( document.getElementById("suggestProject").value, loadinfoProject );			  
}

//<!--  ส่วนที่ 3 -->
function loadinfoProject(project){		
	//dwr.util.setValue("projectCode", project.name);
	dwr.util.setValue("projectName", project.name);			     
}
</script>	

//<!--  ส่วนที่ 2 -->
<script language="JavaScript">
var var1=0;
function getBCPurchaseOrder(id2) {
	var1=id2; 
	if(document.getElementById("ittl_purchaseCode"+id2).value!="") {				
		po.getBCPurchaseOrder( document.getElementById("ittl_purchaseCode"+id2).value, loadinfoPO );
	}			  
}

//<!--  ส่วนที่ 3 -->
function loadinfoPO(po){		
	//dwr.util.setValue("projectCode", project.name);
	if(po.docNo!=null) {
		
		dwr.util.setValue("ittl_purchaseCode"+var1, po.ittl_purchaseCode);
		dwr.util.setValue("docNo"+var1, po.docNo);
		dwr.util.setValue("items"+var1, po.items);
	}else {
		alert("ไม่พบ :"+document.getElementById("ittl_purchaseCode"+var1).value+" \n\n\nกรุณาตรวจสอบเลขที่เอกสารอีกครั้ง");
		//document.getElementById("apCode").focus();
		dwr.util.setValue("ittl_purchaseCode"+var1, "");
		dwr.util.setValue("docNo"+var1, "");
		dwr.util.setValue("items"+var1, "");
		
	}					     
}
</script>


<!--  ส่วนที่ 2 -->
<script language="JavaScript">
var var1=0;
var sumOfItemAmount=0;
var taxAmount = 0;
var totalAmount  =0;
function getBCAPInvoice(id2) {
	var1=id2;
	//alert("bcproject"+form1.projectCode.value); ใช้ใน ff ไม่ผ่าน			
	//alert(document.getElementById("docNo1").value);
	//BCAPInvoiceService2 เป็นชื่อ javascript ที่ อยู่ใน dwr xml ไว้สำหรับอ้างไปที่ java 
	if(document.getElementById("docNo"+id2).value!="") {
		document.getElementById("docNo"+id2).value = document.getElementById("docNo"+id2).value.toUpperCase();
		BCAPInvoiceService2.getBCAPInvoice( document.getElementById("docNo"+id2).value, loadinfoAPInvoice );
	}			  
}

//<!--  ส่วนที่ 3 -->
function loadinfoAPInvoice(bCAPInvoice){
	//alert(bCAPInvoice.sumOfItemAmount);
	//docNo = document.getElementById("docNo"+id).value;
	
	if(bCAPInvoice.sumOfItemAmount!=null) {
		document.getElementById("docNo"+var1).value = bCAPInvoice.docNo;
		docNo = document.getElementById("docNo"+var1).value;
		items 			= bCAPInvoice.numOfItem;
		sumOfItemAmount = sumOfItemAmount*1+ bCAPInvoice.sumOfItemAmount*1;
		taxAmount       = taxAmount*1+ bCAPInvoice.taxAmount*1;
		totalAmount     = totalAmount*1+ bCAPInvoice.totalAmount*1;

		
		dwr.util.setValue("items"+var1, items);
		//alert(docNo.substr(0,1));
		if(docNo.substr(0,1)=="C" ) {
			
			dwr.util.setValue("cash"+var1, "");
			dwr.util.setValue("sumOfItemAmount"+var1, bCAPInvoice.sumOfItemAmount.toFixed(2));

		}else if(docNo.substr(0,1)=="B" ) {
			dwr.util.setValue("cash"+var1, bCAPInvoice.sumOfItemAmount.toFixed(2));
			dwr.util.setValue("sumOfItemAmount"+var1, "");
			
		}
		dwr.util.setValue("taxAmount"+var1, bCAPInvoice.taxAmount.toFixed(2));
		dwr.util.setValue("totalAmount"+var1, bCAPInvoice.totalAmount.toFixed(2));
		dwr.util.setValue("type"+var1, bCAPInvoice.remark2);			

	}else {
		alert("ไม่พบ :"+document.getElementById("docNo"+var1).value+" \n\n\nกรุณาตรวจสอบเลขที่เอกสารอีกครั้ง");
		//document.getElementById("apCode").focus();
		dwr.util.setValue("sumOfItemAmount", "");
	}				     
}
</script>

<script type="text/javascript">
function alertValue() {
	alert(document.getElementById('htmlElementTest').value)
}
</script>

	<link type="text/css" href="<%= request.getContextPath() %>/css/datepicker/css/ui.all.css" rel="stylesheet" /> 
	
	<script type="text/javascript" src="<%= request.getContextPath() %>/css/datepicker/lib/jquery-ui-1.5.3.min.js"></script>
	




</head>

<body><br />
<div id="wrapper">
	 
	<div id="menu"><ul id="nav"></ul></div>

	<div id="content">
		<div id="menuNav"><a href='<%=request.getContextPath() %>/Login.do?mode=check'>หน้าหลัก</a> 
		>> คลังพัสดุ - Store กลาง  >> จัดซื้อ
		>> <a href='<%=request.getContextPath() %>/apinvoice/po.do?mode=blank_po3'>4.บันทึกซื้อสินค้าและบริการ (เลือกใบ PO)</a> 
		>> <a href='<%=request.getContextPath() %>/apinvoice/apinvoice2.do?mode=showFind'>5.ดูรายการซื้อสินค้าและบริการ</a>
		
	</div><br />
			
	<div style="background: #ffffff;">
	<p align="center" id="titleIssueType">กรุณาเลือกใบซื้อ BCAPInvoice</p>
	
	<html:form action="/summaryInv.do?mode=summaryInv" styleId="form1" target="_blank" >	        
                     
    <div id="items"> 
     <fieldset>
     	<legend><b>ใส่เลขใบ BCS, CRS, CMD</b></legend>     	
     	    <input type="hidden" id="id" value="0"> 	     		
     	<center>	     		          	
		<table border="1" cellspacing="10" cellpadding="10">
			
			<tbody id="inputs" >
	 			<tr>
	 				<td ><a href="#" onClick="addFormField(); return false;">
     					<img src="<%=request.getContextPath() %>/common/images/add.png" width="24" height="24" alt="Add" />
     					</a>
     				</td>
     				<td align="center">Date:</td>
     				<td ><input type="text" name="reportDate" id="reportDate" maxlength="10" style="width:140px;height:30px;font-size:120%;font-weight:bold"></td>
     				<td  align="center">Slip No.</td>
     				<td colspan="5">
     					
     						<input type="text" name="slipNo" id="slipNo" size="40" style="height:30px;font-size:120%;font-weight:bold">
     					 
     				</td>				 				
	 			</tr>				
				<tr>
					<td>&nbsp;</td>
					<td width="100" align="center">DocNo/TaxNo/BillGroup</td>
					<td align="center">Type</td>
					<td width="70" align="center">Items</td>
					<td width="100" align="center">Cash</td>
					<td width="100" align="center">Credit</td>
					<td width="100" align="center">Vat 7%</td>
					<td width="100" align="center">Total</td>
					
					
				</tr>
				<tr id="r1" >
					<td ><a href='#' onClick='removeFormField("#r1",1); return false;'>
						<img src="<%=request.getContextPath() %>/common/images/remove.png" width="24" height="24" alt="Remove" /></a></td>
								
				  	<td>				  						  						  		
				  		<input type="text" name="docNoArray" id="docNo1" size='15' onblur="getBCAPInvoice(1);" onchange="getBCAPInvoice(1);" >				  		
				  	</td>
				  	<td align="center">
				  		<input type="text" name="typeArray" id="type1" maxlength="3" size="5" onfocus='addFormField(); return false;'>
				  	</td>				  	
				  	<td align="right" valign="middle">
				  		<div align="right" id="items1"></div> 
				  	</td>
				  	<td align="right" valign="middle">
				  		<div align="right" id="cash1"></div> 
				  	</td>				  					  	
				  	<td align="right" valign="middle">
				  		<div align="right" id="sumOfItemAmount1"></div> 
				  	</td>
				  	<td align="right" valign="middle">
				  		<div align="right" id="taxAmount1"></div> 
				  	</td>
				  	<td align="right" valign="middle">
				  		<div align="right" id="totalAmount1"></div> 
				  	</td>				  					  	

				  											
				</tr>
			</tbody>
			<tfoot>
				<tr>
					<td>&nbsp;</td>
					<td width="100" align="center">&nbsp;</td>
				  	<td align="right" valign="middle">
				  		<div align="right" id="sumOfItemAmount"></div> 
				  	</td>
				  	<td align="right" valign="middle">
				  		<div align="right" id="taxAmount"></div> 
				  	</td>
				  	<td align="right" valign="middle">
				  		<div align="right" id="totalAmount"></div> 
				  	</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					
				</tr>			
			</tfoot>				
		</table>
		</center><br></br>     	
		<!--<font size='2'>หมายเหตุ : <font color="red">*</font> คือข้อมูลที่ต้องใส่</font>
     --></fieldset>
 	</div>
 	
 	<table border="0" align="center" width="100%">
 		<tr>
 			<td align="center" ><html:submit value="คลิกเพื่อดึงข้อมูล"  /></td>
 			<td align="center" ></td>
			<td align="center" ><html:button  value="กลับไปรายการเมนู" property="show1" 
 			onclick="gotoMenu()"  /></td> 			
 		</tr>
 	</table>    	    	    	
    </html:form><br>
    </div>

<script type="text/javascript">

var item2 = document.getElementById("item2").value;
var item3 = document.getElementById("item3").value;
var item4 = document.getElementById("item4").value;

	if(item2=="") {
		idArray[1] = 0;
	}
	if(item3=="") {
		idArray[2] = 0;
	}
	if(item4=="") {
		idArray[3] = 0;
	}	

function refreshForm() {
	var lAddr = document.getElementById("localAddr").value;
	var lPort = document.getElementById("localPort").value;
	var ctPath = document.getElementById("contextPath").value;
	var docNo = document.getElementById("docNo").value
	window.location = "http://"+lAddr+":"+lPort+ctPath+"/StkReq.do?mode=edit&docNo="+docNo;	
}
function gotoMenu() {
	var lAddr = document.getElementById("localAddr").value;
	var lPort = document.getElementById("localPort").value;
	var ctPath = document.getElementById("contextPath").value;
	window.location = "http://"+lAddr+":"+lPort+ctPath+"/Login.do?mode=check";
}
</script>
<input type="hidden" value="<%=request.getLocalAddr() %>" id="localAddr">
<input type="hidden" value="<%=request.getLocalPort() %>" id="localPort">
<input type="hidden" value="<%=request.getContextPath() %>" id="contextPath">

<!-- 
<a href="#" class="add"><img src="add.png" width="24" height="24" alt="add" title="add input" /></a> 
<a href="#" class="reset"><img src="reset.png" width="24" height="24" alt="reset" /></a>
 -->
 </div>
 </div>
</body>
</html:html>